﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using OrmAuto;
using AjaxControlToolkit;
using System.Data;
using System.Data.SqlClient;

public partial class Contents_InputAgenti : System.Web.UI.Page
{


		protected void Page_Load(object sender, EventArgs e)
		{
				if (!this.Page.User.Identity.IsAuthenticated)
						Response.Redirect(FormsAuthentication.LoginUrl); //	
						
				if(!IsPostBack)
						inizializza();		

		}

		private void inizializza()
		{
		  QuerierLight qL = new QuerierLight(); 
		  
				this.MarcheDrop.DataSource= qL.getMarche() ;
				this.MarcheDrop.DataBind();
				
				this.PossessiDrop.DataSource = qL.getPossessi();
				this.PossessiDrop.DataBind();  

				var idU = qL.getUtenteID(this.Page.User.Identity.Name);
				myIdU.Value = idU.ToString();
				
		}//fM

		public void showVistaAnnunci(bool pVendita, int pIdUtente, int pIdMarca,int pIdPossesso, bool pAbilitato)
  {
				RicercheSql query = new RicercheSql();
				
				List<SqlParameter> parametri = new List<SqlParameter>(2);
				parametri.Add(new SqlParameter("@UserId", SqlDbType.Int) { Value = pIdUtente });
				parametri.Add(new SqlParameter("@IdMarca", SqlDbType.Int) { Value = pIdMarca });
				parametri.Add(new SqlParameter("@IdPossesso", SqlDbType.Int) { Value = pIdPossesso });
				parametri.Add(new SqlParameter("@Abilitato", SqlDbType.Bit) { Value = pAbilitato });
				
				
				var dati = query.execStoredProc (OggettiDB.spGetANNacquistoByVarie,  parametri.ToArray());  
				this.GridViewAnnunci.DataSource = dati ;
				this.GridViewAnnunci.DataBind();
				 
  }//fM


		[System.Web.Services.WebMethod]
		[System.Web.Script.Services.ScriptMethod]
		public static CascadingDropDownNameValue[] GetDropDownContents(
									string knownCategoryValues, string category)
		{
				QueryLuoghi mQuery = new QueryLuoghi();
				var arrCatena = knownCategoryValues.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries);

				if (category.Equals("StatoLuogoMerce"))
				{
						if (arrCatena == null) return new CascadingDropDownNameValue[0];

						var arr = arrCatena[0].Split(new[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
						var idStato = int.Parse(arr[1]);
						var regioni = mQuery.getRegioni(idStato).Select(
								r => new CascadingDropDownNameValue() { name = r.REG_REGIONE, value = r.REG_ID.ToString() }).ToArray();
						return regioni;
				}

				if (category.Equals("RegioneLuogoMerce"))
				{
						if (arrCatena == null) return new CascadingDropDownNameValue[0];

						var arr1 = arrCatena[0].Split(new[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
						var arr2 = arrCatena[1].Split(new[] { ":" }, StringSplitOptions.RemoveEmptyEntries);

						var idStato = int.Parse(arr1[1]);
						var idRegioni = -1;
						if (!int.TryParse(arr2[1], out idRegioni)) return new CascadingDropDownNameValue[0];

						var provincie = mQuery.getProvince(idRegioni).Select(
								p => new CascadingDropDownNameValue() { name = p.PROV_PROVINCIA, value = p.PROV_ID.ToString() }).ToArray();
						return provincie;
				}

				return new CascadingDropDownNameValue[0];
		}


		[System.Web.Services.WebMethod]
		[System.Web.Script.Services.ScriptMethod]
		public static CascadingDropDownNameValue[] GetDropDownModelli(
									string knownCategoryValues, string category)
		{
				var arrCatena = knownCategoryValues.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries);

				if (category.Equals("Modelli"))
				{
						if (arrCatena == null) return new CascadingDropDownNameValue[0];
						QuerierMarcheModelli mQuery = new QuerierMarcheModelli();

						var arr = arrCatena[0].Split(new[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
						//var idmarca = int.Parse(arr[1]);
						var marca = arr[1];
						int idMarca = 0;
						int.TryParse(marca, out idMarca);
						var modelli = mQuery.getModelli(idMarca).Select(
								m => new CascadingDropDownNameValue() { name = m.Modello, value = m.ID.ToString() }).ToList();
						//modelli.Insert(0, new CascadingDropDownNameValue { isDefaultValue = false, name = "Tutti/All", value = "%%" });
						return modelli.ToArray();
				}

				return new CascadingDropDownNameValue[0];
		}//fM


		protected void SqlDataSource_AnnVendita_Inserting(object sender, SqlDataSourceCommandEventArgs e)
		{
				var c = e.Command;
		}
		protected void SqlDataSource_AnnVendita_Inserted(object sender, SqlDataSourceStatusEventArgs e)
		{
				var ex = e.Exception;
		}//fM
		
		
		//annunci di vendita
		protected void ShowButton_Click(object sender, ImageClickEventArgs e)
		{
		  var idmarca = this.MarcheDrop.SelectedValue;  
		  var idpossesso = this.PossessiDrop.SelectedValue;
		  bool abilitato = this.AbilitatoCheckBox.Checked; 
		  
		  int idMarca =0,idPossesso;
		  int.TryParse(idmarca,out idMarca);
		  int.TryParse(idpossesso,out idPossesso);

				QuerierLight qL = new QuerierLight(); 
				var idUtente = qL.getUtenteID(Page.User.Identity.Name); 
				
				if( !(myIdU.Value == idUtente.ToString()))
						Response.Redirect("~/Genericerror.aspx?error="+ Server.UrlEncode("Utente non identificato."));
						  
				showVistaAnnunci(true,idUtente,idMarca,idPossesso,abilitato);
				
		}//fM
		
		
		protected void GridViewAnnunci_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
		{
				    
		}//fM
		
		protected void GridViewAnnunci_SelectedIndexChanged(object sender, EventArgs e)
		{
		  var grid = ((GridView)sender);
		  if(grid.SelectedDataKey == null) return ;
				var ID =grid.SelectedDataKey.Value;
				if(ID == null) return ;
				var id = (int)ID;
				myAnnuncioSql1.AggiornaAnnuncio(id);
				
				tabContainer1.ActiveTabIndex  = 1; 

		}//fM
		
		
		protected void NewButton_Click(object sender, ImageClickEventArgs e)
		{
				myAnnuncioSql1.NuovoAnnuncio();
				tabContainer1.ActiveTabIndex = 2;
		}//fM
		
		////////
		protected void GridViewAnnunci_Sorting(object sender, GridViewSortEventArgs e)
		{
				var sort=
						e.SortDirection == SortDirection.Ascending  ? SortDirection.Descending : SortDirection.Ascending ;  
		 
		 GridViewAnnunci.DataBind(); 
		   //((GridView)sender).Sort(e.SortExpression , sort); 
		
		}

		protected void GridViewAnnunci_Sorted(object sender, EventArgs e)
		{

		}
		
}//fC
